package com.cet.algorithm.栈与队列;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @program: algorithm
 * @description: 删除字符串中的所有相邻重复项
 * @author: 陈恩涛
 * @create: 2022-07-11 16:24
 **/
public class LC1047 {

    public static void main(String[] args) {
        System.out.println(removeDuplicates("abbaca"));
    }

    public static String removeDuplicates(String s) {
        final Deque<Character> deque = new ArrayDeque<>();
        final char[] charArray = s.toCharArray();
        for (char c : charArray) {
            if (deque.isEmpty()) {
                deque.push(c);
            } else {
                if (c == deque.peek()) {
                    deque.pop();
                } else {
                    deque.push(c);
                }
            }
        }
        final StringBuilder sb = new StringBuilder();
        while (!deque.isEmpty()) {
            sb.append(deque.removeLast());
        }
        return sb.toString();
    }
}
